﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XDesigner.ORM;
using 刘颜红销售项目管理.Business.DOM;
using XDesigner.AppFramework;
using XDesigner.AppFramework.WinForms;
using 刘颜红销售项目管理.Business;

namespace 刘颜红销售项目管理
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private MdiChildrenManager _ChildForms = new MdiChildrenManager();
        private void frmMain_Load(object sender, EventArgs e)
        {
            tvwPeople.ShowActionNode = true;
            _ChildForms.MdiParent = this;
            myTabbedMdiManager.MdiParent = this;
             
             
            RefreshPeopleTree();

            myTreeControl_AfterSelect(null, null);

        }
          

        private void RefreshPeopleTree()
        {
            this.Text = "销售项目管理 - " + AppSettings.Default.DBFileName;
            tvwPeople.RefreshView();
            myTreeControl_AfterSelect(null, null);

            if (string.IsNullOrEmpty(MyApplicationFramework.Instance.NotifyMessage) == false)
            {
                btnNotifyMessage.Visible = true;
            }
            else
            {
                btnNotifyMessage.Visible = false;
            }
        }

        private void myTreeControl_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if ( tvwPeople.SelectedValue == null)
            {
                return;
            }
            if (tvwPeople.SelectedValue is Class工作单位)
            {
                Class工作单位 unit = (Class工作单位)tvwPeople.SelectedValue;
                ORMBindingSource bs = ORMBindingSource.CreatePackage(unit, typeof(Class人员基本信息), unit.人员列表);
                bs.Engine = MyApplicationFramework.Instance.ORMEngine;
                bs.ORMType = MyApplicationFramework.Instance.ORMEngine.Project.Types[typeof(Class人员基本信息)];
                _ChildForms.CloseAll(new Type[] { typeof(frm人员列表) });

                frm人员列表 frm2 = (frm人员列表)_ChildForms.FindChildForm(typeof(frm人员列表), true);
                frm2.当前单位 = unit;
                frm2.Show();
                frm2.RefreshView();
            }
            else if (tvwPeople.SelectedValue is Class项目基本信息)
            {
                Class项目基本信息 project = (Class项目基本信息)tvwPeople.SelectedValue;
                _ChildForms.CloseAll(new Type[] { typeof(frm订购机型表) });
                frm订购机型表 frm2 = (frm订购机型表)_ChildForms.FindChildForm(typeof(frm订购机型表), true);
                frm2.项目对象 = project;
                frm2.Show();
                frm2.RefreshView();
            }
            else if (tvwPeople.Controler.SelectedValue is Class人员基本信息)
            {
                Class人员基本信息 people = (Class人员基本信息)tvwPeople.SelectedValue;

                _ChildForms.CloseAll(new Type[] { typeof(frm人员详细信息) });

                frm人员详细信息 frm2 = (frm人员详细信息)_ChildForms.FindChildForm(typeof(frm人员详细信息), true);
                frm2.People = people;
                frm2.Show();
                frm2.RefreshView();
            }
            else if (tvwPeople.SelectedValue is MyApplicationFramework)
            {
                _ChildForms.CloseAll(new Type[] {
                    typeof( frm单位基本信息2 )});

                frm单位基本信息2 frm2 = (frm单位基本信息2)_ChildForms.FindChildForm(typeof(frm单位基本信息2), true);
                frm2.DataSource = MyApplicationFramework.Instance.单位列表;
                frm2.Show();
                frm2.RefreshView();

            }
            else if (tvwPeople.SelectedValue == StructureTreeProvider._产品型号)
            {
                _ChildForms.CloseAll(new Type[] { typeof(frm产品型号基本信息) });
                frm产品型号基本信息 frm = (frm产品型号基本信息)_ChildForms.FindChildForm(typeof(frm产品型号基本信息), true);
                frm.Show();
            }
            else if (tvwPeople.SelectedValue == StructureTreeProvider._基础代码维护)
            {
                _ChildForms.CloseAll(new Type[] { typeof(frm基础代码维护) });
                frm基础代码维护 frm = (frm基础代码维护)_ChildForms.FindChildForm(typeof(frm基础代码维护), true);
                frm.Show();
            }
            else if (tvwPeople.SelectedValue == StructureTreeProvider._项目基本信息)
            {
                _ChildForms.CloseAll(new Type[] { typeof(frm项目列表) });
                frm项目列表 frm = (frm项目列表)_ChildForms.FindChildForm(typeof(frm项目列表), true);

                frm.Show();
            }
            else if (tvwPeople.SelectedValue == StructureTreeProvider._工作日志)
            {
                _ChildForms.CloseAll(new Type[] { typeof(frm工作日志) });
                frm工作日志 frm = (frm工作日志)_ChildForms.FindChildForm(typeof(frm工作日志), true);
                frm.Show();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            MyApplicationFramework.Instance.ClearBuffer();
            RefreshPeopleTree();
            this.Cursor = Cursors.Default;
        }

        private SearchResultEnumerator mySearchResult = new SearchResultEnumerator();
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string text = txtSearch.Text.Trim();
            if (text.Length > 0)
            {
                this.Cursor = Cursors.WaitCursor;
                if (mySearchResult.SetCondition(text))
                {
                    tvwPeople.Controler.EnumerateNode(delegate(object sender2, TreeViewCancelEventArgs args) 
                        {
                            string text2 = args.Node.Text;
                            if (text2 != null)
                            {
                                if (text2.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0)
                                {
                                    mySearchResult.AddResult(args.Node.Tag );
                                }
                            }
                        });
                }//if
                if (mySearchResult.MoveNext())
                {
                    TreeNode node = tvwPeople.Controler.GetNode(mySearchResult.Current);
                    if (node != null)
                    {
                        this.tvwPeople.SelectedNode = node;
                        node.EnsureVisible();
                        this.tvwPeople.Focus();
                    }
                }//if
                else
                {
                    MessageBox.Show("没有找到指定的记录!");
                    mySearchResult.Clear();
                }
                this.Cursor = Cursors.Default;
            }//if
        }

        private void btnORMDebugger_Click(object sender, EventArgs e)
        {
            using (frmORMDebugger dlg = new frmORMDebugger())
            {
                dlg.ShowDialog(this);
            }
        }

        private void btnNotifyMessage_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
                this, 
                MyApplicationFramework.Instance.NotifyMessage,
                this.Text,
                MessageBoxButtons.OK, 
                MessageBoxIcon.Information);
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            using (dlgConfig dlg = new dlgConfig())
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    MyApplicationFramework.Instance.DataBase.Close();
                    MyApplicationFramework.Instance.DataBase.Connection = Program.CreateConnection();
                    btnRefresh_Click(null, null);
                }
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "本程序由刘颜红需求指导和设计，袁永福开发。本程序仅供学习和参考使用，不得直接用于商业。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}